clear all
set maxvar 32767

global workdata = "O:\BSS_TU-2020\workdata\"

*2001
use "$workdata\dk_diaries_2001.dta", clear 
keep lbnr sex psex v115 v170 partner hpact* phpact*

keep if partner == 1 /*keep only couples*/
drop partner 

rename psex sex_spouse  
drop if sex + sex_spouse != 3

gen supervisor = (v115 >= 2 & v115 < 6 & v115 != .)  /*super with subordinates*/
gen supervisor_spouse = (v170 >= 2 & v170 < 6 & v170 != .)  /*spouse super with subordinates*/
drop v170 v115

*Drop if not working
forvalues i = 1/144 {
	gen work`i' = (hpact`i' >= 100 & hpact`i' <200)
	}

gen sum_work = 0
forvalues i = 1/144 {
	replace sum_work = sum_work + work`i'
	}

forvalues i = 1/144 {
	gen pwork`i' = (phpact`i' >= 100 & phpact`i' <200)
	}

gen sum_pwork = 0
forvalues i = 1/144 {
	replace sum_pwork = sum_pwork + pwork`i'
	}	
	
drop if sum_work == 0  
drop if sum_pwork == 0  

drop work* 
save "$workdata\All_2001.dta", replace

*SUPERVISORS WITH NON-SUPERVISOR SPOUSES

*Focus on IP
use "$workdata\All_2001.dta", clear
keep if supervisor == 1 & supervisor_spouse == 0

*Male supervisor
preserve
keep if sex == 1 
keep hpact* phpact*
save "$workdata\MS1_data.dta", replace
restore

*Women supervisor
preserve
keep if sex == 2 
keep hpact* phpact*
save "$workdata\WS1_data.dta", replace
restore

*Focus on spouse observation
use "$workdata\All_2001.dta", clear
keep if supervisor == 0 & supervisor_spouse == 1

*make spouse observation first observation
forvalues i = 1/144 {
	rename hpact`i' x_hpact`i'
	}

forvalues i = 1/144 {
	rename phpact`i' hpact`i'
	}

forvalues i = 1/144 {
	rename x_hpact`i' phpact`i'
	}

*Male supervisor
preserve
keep if sex_spouse == 1 
keep hpact* phpact*
save "$workdata\MS2_data.dta", replace
restore

*Women supervisor
preserve
keep if sex_spouse == 2 
keep hpact* phpact*
save "$workdata\WS2_data.dta", replace
restore

*append data 
use "$workdata\MS1_data.dta", clear
append using "$workdata\MS2_data.dta"
save "$workdata\MS_data_2001.dta", replace

use "$workdata\WS1_data.dta", clear
append using "$workdata\WS2_data.dta"
save "$workdata\WS_data_2001.dta", replace

*PRODUCE DATA FOR MERGE WITH 2008 DATA

*Husband supervisor, wife not supervisor
use "$workdata\MS_data_2001.dta", clear
forvalues i = 1/144 {
	gen work`i' = (hpact`i' >=100 & hpact`i' <200)
	}
keep work*
save "$workdata\HusbandSuper_WifeNotsuper_2001.dta", replace

*Wife not supervisor, Husband supervisor
use "$workdata\MS_data_2001.dta", clear
forvalues i = 1/144 {
	gen work`i' = (phpact`i' >=100 & phpact`i' <200)
	}
keep work*
save "$workdata\WifeNotsuper_HusbandSuper_2001.dta", replace

*Wife supervisor, husband not supervisor
use "$workdata\WS_data_2001.dta", clear
forvalues i = 1/144 {
	gen work`i' = (hpact`i' >=100 & hpact`i' <200)
	}
keep work*
save "$workdata\WifeSuper_HusbandNotsuper_2001.dta", replace

*Husband not supervisor, Wife supervisor
use "$workdata\WS_data_2001.dta", clear

forvalues i = 1/144 {
	gen work`i' = (phpact`i' >=100 & phpact`i' <200)
	}
keep work*
save "$workdata\HusbandNotsuper_WifeSuper_2001.dta", replace

*POWER COUPLES
use "$workdata\All_2001.dta", clear
keep if supervisor == 1 & supervisor_spouse == 1

*IP
preserve
forvalues i = 1/144 {
	gen work`i' = (hpact`i' >=100 & hpact`i' <200)
	}
keep work* sex
save "$workdata\PC_IP_2001.dta", replace
restore

*Spouse
preserve
forvalues i = 1/144 {
	gen work`i' = (phpact`i' >=100 & phpact`i' <200)
	}
drop sex
rename sex_spouse sex
keep work* sex
save "$workdata\PC_spouse_2001.dta", replace
restore

use "$workdata\PC_IP_2001.dta", clear
append using "$workdata\PC_spouse_2001.dta"

preserve
keep if sex == 1 /*men*/
drop sex
save "$workdata\PowerCouple_men_2001.dta", replace
sum
restore

preserve
keep if sex == 2 /*women*/
drop sex
save "$workdata\PowerCouple_women_2001.dta", replace
sum
restore



*2008
use "$workdata\DTUC2008 - Baggrund.dta" , clear
keep fprimary ip_koen p1_koen spm12ip spm12sam spm12aip spm12asam p1_ip_relation 

keep if p1_ip_relation == 1  /*keep only couples*/
drop p1_ip_relation

rename ip_koen sex
rename p1_koen sex_spouse
drop if sex+sex_spouse !=3

gen supervisor = (spm12ip ==2)  /*super with subordinates*/
gen supervisor_spouse = (spm12sam ==2)   /*spouse super with subordinates*/

drop spm*
save "$workdata\background_all_2008.dta", replace

*IP
use "$workdata\background_all_2008.dta", clear
gen instancenumber = 2  /*indicator for weekday activity for IP*/
save "$workdata\background_IP_2008.dta", replace

*Spouse
use "$workdata\background_all_2008.dta", clear
gen instancenumber = 4  /*Indicator for weekday activity for spouse*/
rename sex sex_x
rename sex_spouse sex
rename sex_x sex_spouse
rename supervisor supervisor_x
rename supervisor_spouse supervisor
rename supervisor_x supervisor_spouse
save "$workdata\background_spouse_2008.dta", replace

*append and merge with time use data
use "$workdata\background_IP_2008.dta", clear
append using "$workdata\background_spouse_2008.dta"
merge 1:1 fprimary instancenumber using "$workdata\DTUC2008 - Tid.dta"
keep if _m == 3
drop _m
keep sex* super* int* fprimary
drop inter intgo

*Drop if not working
forvalues i = 1/144 {
	gen work`i' = (int`i' >= 4 & int`i' <= 7)
	}

gen sum_work = 0
forvalues i = 1/144 {
	replace sum_work = sum_work + work`i'
	}
drop if sum_work == 0
drop work*
save "$workdata\all_couples_2008.dta", replace

*Husband supervisor, wife not supervisor 
use "$workdata\all_couples_2008.dta", clear
keep if sex == 1 & supervisor == 1 & supervisor_spouse == 0 
forvalues i = 1/144 {
	gen work`i' = (int`i' >= 4 & int`i' <= 7)
	}
keep work*
save "$workdata\HusbandSuper_WifeNotsuper_2008.dta", replace

*Wife supervisor, husband not supervisor
use "$workdata\all_couples_2008.dta", clear
keep if sex == 2 & supervisor == 1 & supervisor_spouse == 0 
forvalues i = 1/144 {
	gen work`i' = (int`i' >= 4 & int`i' <= 7)
	}
keep work*
save "$workdata\WifeSuper_HusbandNotsuper_2008.dta", replace

*Husband not supervisor, Wife supervisor
use "$workdata\all_couples_2008.dta", clear
keep if sex == 1 & supervisor == 0 & supervisor_spouse == 1 
forvalues i = 1/144 {
	gen work`i' = (int`i' >= 4 & int`i' <= 7)
	}
keep work*
save "$workdata\HusbandNotsuper_WifeSuper_2008.dta", replace

*Wife not supervisor, Husband supervisor
use "$workdata\all_couples_2008.dta", clear
keep if sex == 2 & supervisor == 0 & supervisor_spouse == 1
forvalues i = 1/144 {
	gen work`i' = (int`i' >= 4 & int`i' <= 7)
	}
keep work*
save "$workdata\WifeNotsuper_HusbandSuper_2008.dta", replace

*Power couples
use "$workdata\all_couples_2008.dta", clear
keep if supervisor == 1 & supervisor_spouse == 1 

*Men
preserve
keep if sex == 1
forvalues i = 1/144 {
	gen work`i' = (int`i' >= 4 & int`i' <= 7)
	}
keep work* 
save "$workdata\PowerCouple_men_2008.dta", replace
restore

*Women
preserve
keep if sex == 2
forvalues i = 1/144 {
	gen work`i' = (int`i' >= 4 & int`i' <= 7)
	}
keep work* 
save "$workdata\PowerCouple_women_2008.dta", replace
restore


*Figures
use "$workdata\HusbandSuper_WifeNotsuper_2001.dta", clear
append using "$workdata\HusbandSuper_WifeNotsuper_2008.dta"
mat define figure = J(144,2,0)
mat colnames figure = obs working 
forvalues i = 1/144 {
	mat figure[`i',1]= `i'
	sum work`i'
	mat figure[`i',2]= r(mean)
}
mat list figure
drop *
svmat double figure, names(col)
rename working w_HS_WNS
sort obs
save "$workdata\profile_HS_WNS.dta", replace

use "$workdata\WifeNotsuper_HusbandSuper_2001.dta", clear
append using "$workdata\WifeNotsuper_HusbandSuper_2008.dta"
mat define figure = J(144,2,0)
mat colnames figure = obs working 
forvalues i = 1/144 {
	mat figure[`i',1]= `i'
	sum work`i'
	mat figure[`i',2]= r(mean)
}
mat list figure
drop *
svmat double figure, names(col)
rename working w_WNS_HS
sort obs
save "$workdata\profile_WNS_HS.dta", replace

use "$workdata\WifeSuper_HusbandNotsuper_2001.dta", clear
append using "$workdata\WifeSuper_HusbandNotsuper_2008.dta"
mat define figure = J(144,2,0)
mat colnames figure = obs working 
forvalues i = 1/144 {
	mat figure[`i',1]= `i'
	sum work`i'
	mat figure[`i',2]= r(mean)
}
mat list figure
drop *
svmat double figure, names(col)
rename working w_WS_HNS
sort obs
save "$workdata\profile_WS_HNS.dta", replace

use "$workdata\HusbandNotsuper_WifeSuper_2001.dta", clear
append using "$workdata\HusbandNotsuper_WifeSuper_2008.dta"
mat define figure = J(144,2,0)
mat colnames figure = obs working 
forvalues i = 1/144 {
	mat figure[`i',1]= `i'
	sum work`i'
	mat figure[`i',2]= r(mean)
}
mat list figure
drop *
svmat double figure, names(col)
rename working w_HNS_WS
sort obs
save "$workdata\profile_HNS_WS.dta", replace

*FIGURE 3A
use "$workdata\profile_HS_WNS.dta", clear
merge 1:1 obs using "$workdata\profile_WNS_HS.dta"

line w_HS_WNS obs, legend(label(1 "Married men, supervisor")) lc(black) lp(-) || ///
line w_WNS_HS obs, legend(label(2 "Spouse to male supervisor")) lc(gray)   /// 
xla(1 "4 a.m." 25 "8 a.m" 49 "noon" 73 "4 p.m." 97 "8 p.m." 121 "12 p.m." 144 "4 a.m.") ///
ytitle("Proportion working") xtitle("Time of day") yscale(r(0.8)) ylabel(0(0.2)1) 

*FIGURE 3B
use "$workdata\profile_WS_HNS.dta", clear
merge 1:1 obs using "$workdata\profile_HNS_WS.dta"

line w_WS_HNS obs, legend(label(1 "Married women, supervisor")) lc(black) lp(-) || ///
line w_HNS_WS obs, legend(label(2 "Spouse to female supervisor")) lc(gray)   /// 
xla(1 "4 a.m." 25 "8 a.m" 49 "noon" 73 "4 p.m." 97 "8 p.m." 121 "12 p.m." 144 "4 a.m.") ///
ytitle("Proportion working") xtitle("Time of day") yscale(r(0.8)) ylabel(0(0.2)1) 

*Figure 3 - power couples
use "$workdata\PowerCouple_men_2001.dta", clear
append using "$workdata\PowerCouple_men_2008.dta"

mat define figure = J(144,2,0)
mat colnames figure = obs working 
forvalues i = 1/144 {
	mat figure[`i',1]= `i'
	sum work`i'
	mat figure[`i',2]= r(mean)
}
mat list figure
drop *
svmat double figure, names(col)
rename working w_PC_men
sort obs
save "$workdata\profile_PC_men.dta", replace

use "$workdata\PowerCouple_women_2001.dta", clear
append using "$workdata\PowerCouple_women_2008.dta"

mat define figure = J(144,2,0)
mat colnames figure = obs working 
forvalues i = 1/144 {
	mat figure[`i',1]= `i'
	sum work`i'
	mat figure[`i',2]= r(mean)
}
mat list figure
drop *
svmat double figure, names(col)
rename working w_PC_women
sort obs
save "$workdata\profile_PC_women.dta", replace


*Figure 4
use "$workdata\profile_PC_men.dta", clear
merge 1:1 obs using "$workdata\profile_PC_women.dta"

line w_PC_men obs, legend(label(1 "Married men, supervisor")) lc(black) lp(-) || ///
line w_PC_women obs, legend(label(2 "Married women, supervisor")) lc(gray)   /// 
xla(1 "4 a.m." 25 "8 a.m" 49 "noon" 73 "4 p.m." 97 "8 p.m." 121 "12 p.m." 144 "4 a.m.") ///
ytitle("Proportion working") xtitle("Time of day") yscale(r(0.8)) ylabel(0(0.2)1) 






















